/**
 * Created by yuzhou on 16/9/23.
 */

define([
    'vue',
    'vuex/mutation-types',
    'api/good'
], function(Vue, types, goodApi) {

    return {
        getAllGood: function(store) {
            goodApi.getAll(function(all, ts) {
                store.dispatch(types.RECEIVE_ALL_GOOD, all, ts)
            })
        },
        saveGood: function(store, good, cb, errorCb) {
            goodApi.saveGood(good, function(good){
                cb(good)
                store.dispatch(types.RESET_LOAD_TIME)
            }, errorCb)
        },
        getTypes: function(store, force){
            if(store.state.good.types == null || force) {
                goodApi.getTypes(function(goodTypes){
                    store.dispatch(types.RECEIVE_GOOD_TYPES, goodTypes)
                }, function(error){
                    alert(error)
                })
            }
        },
        getBrands: function(store, goodType) {
            goodApi.getBrandsByType(goodType, function(brands){
                store.dispatch(types.RECEIVE_GOOD_BRANDS, goodType, brands)
            }, function(error){
                alert(error)
            })
        },
        getExtGoods: function(store, brandId) {
            goodApi.getExtGoodsByBrand(brandId, function(extGoods){
                store.dispatch(types.RECEIVE_EXT_GOODS, brandId, extGoods)
            }, function(error){
                alert(error)
            })
        }
    }
})